// Create english dummy text
// Date 17:59 12/12/2016
// By Ben a.k.a DreamVB

#include <iostream>
#include <vector>
#include <time.h>

using namespace std;
using std::cout;
using std::endl;

vector<string>words;

const char* ErrMsgInvaild = "Invaild value entered.\n";

string GetRandomPar(int wordcount){
	string buffer = "";
	int j = 0;
	int r = 0;

	while (j < wordcount){
		r = rand() % words.size();
		buffer += words[r] + " ";
		j++;
	}
	return buffer;
}

void generate(){
	int r = 0;
	int i = 0;
	int lines = -1;
	string src = "";

	top:
	while (lines == -1){
		system("cls");
		//Get number of paragraphs to make
		cout << "Enter number of paragraphs : ";
		cin >> lines;
		if (lines == -1){
			//Display error message.
			cout << ErrMsgInvaild;
			//Clear up and exit.
			system("pause");
			cin.ignore();
			cin.clear();
			break;
		}
	}

	if (lines == -1){
		return;
	}

	//Clear the screen.
	system("cls");

	//Display random text.
	while (i < lines){
		//Get a set of random words.
		//int randNum = rand()%(max-min + 1) + min;
		r = rand() % (40 - 18 + 1) + 18;
		src = GetRandomPar(r);
		//Print out the random string
		cout << src.c_str() << endl << endl;
		//INC counter
		i++;
	}
	system("pause");
}

void about(){
	system("cls");
	cout << "--- About ---\n\n";
	cout << "\tDummy place holder text generator.\n";
	cout << "\tVersion 1.0\n\t\By DreamVB.\n\n";
	system("pause");
}


int main(int argc, char *argv[]){

	char *lzFile = "Words.txt";
	bool running = true;
	FILE *fp = NULL;
	char sword[20];
	char c = '\0';

	//Open the file.
	fp = fopen("Words.dat", "r");
	
	if (!fp){
		std::cout << "Unable to locate word list.";
		exit(1);
	}

	//Read in the file.
	while (!feof(fp)){
		fgets(sword, 20, fp);
		if (!feof(fp)){
			if (sword[strlen(sword) - 1] != '\r'){
				sword[strlen(sword)] = '\0';
				sword[strlen(sword)- 1] = '\0';
				if (strlen(sword) > 0){
					words.push_back(sword);
				}
			}
		}
	}

	fclose(fp);

	//Get random seed.
	srand(time(0));

	while (running){
		system("cls");
		cout << "+---- Dummy English Text Generator ----+\n";
		cout << "| (G) Generate                         |\n";
		cout << "| (A) About                            |\n";
		cout << "| (Q) Exit                             |\n";
		cout << "+--------------------------------------+\n";
		cout << "Enter choice : ";
		cin >> c;
		c = toupper(c);

		switch (c){
			case 'G':
				//Generate random text.
				generate();
				break;
			case 'A':
				//Show about screen.
				about();
				break;
			case 'Q':
				//Display finish message.
				cout << "Thanks for using the program.\n";
				running = false;
				break;
			default:
				cout << ErrMsgInvaild;
				system("pause");
		}
		cin.ignore();
		cin.clear();
	}

	system("pause");

	return EXIT_SUCCESS;
}